package com.tchintech.pay.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.tchintech.pay.R;
import com.tchintech.pay.adapter.BankAddressAdapter;
import com.tchintech.pay.delegate.QuickPayOneDeleagte;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.model.entity.BankAdress;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.net.OkhttpClientTest;
import com.tchintech.pay.utils.EditUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/7/11 0011.
 * //省市选择测试页面
 */

public class QuickPayOneActivity extends ActivityPresenter<QuickPayOneDeleagte> implements View.OnClickListener {

    private Intent intent;
    private Map<String, String> map;
    private String Url_pro = "Bound.queryPro";
    private String Url_city = "Bound.queryCity";
    private BankAddressAdapter bankAddressAdapter;
    private Handler handler;
    private ListView lv_qac;
    private List<BankAdress> list;
    //在不退出当前页面的情况下保存首次查询的省，避免重复查询
    private boolean isCity = false;
    private List<BankAdress> listPro;
    private String pro;


    @Override
    protected void initView() {
        super.initView();
        viewDelegate.setOnClickListener(this, R.id.iv_back);
        viewDelegate.setTitle("快捷支付");
        viewDelegate.setVisible();
        lv_qac = viewDelegate.get(R.id.lv_qac);
        bankAddressAdapter = new BankAddressAdapter(this, list);
        lv_qac.setAdapter(bankAddressAdapter);
        Listener();
        handler();
        queryPro();

    }

    private void Listener() {
        lv_qac.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BankAdress bankAdress = (BankAdress) parent.getItemAtPosition(position);
                if (isCity) {
                    String city = bankAdress.getName();
                    showMsg(pro + "-" + city);
                } else {

                    pro = bankAdress.getName();
                    queryCity(pro);
                }
            }
        });

    }

    @Override
    protected void initData() {
        super.initData();
        intent = new Intent();
        list = new ArrayList<BankAdress>();
        listPro = new ArrayList<BankAdress>();
    }

    /**
     * 查询省
     */
    private void queryPro() {
        map = new HashMap<String, String>();
        showProgressDialog("", "加载中，请稍后...");
        new OkhttpClientTest().path(this, handler, Url_pro, map, HttpCode.QUERY_PRO);
    }

    /**
     * 查询市
     */
    private void queryCity(String pro) {
        map = new HashMap<String, String>();
        map.put("pro", pro);
        showProgressDialog("", "加载中，请稍后...");
        new OkhttpClientTest().path(this, handler, Url_city, map, HttpCode.QUERY_CITY);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                if (isCity) {
                    isCity = false;
                    setList(listPro);
                } else {
                    finish();
                }
                break;
        }
    }


    private void handler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                hideProgressDialog();
                switch (msg.what) {
                    case HttpCode.networkabnormal:
                        showMsg("当前网络不可用");
                        break;
                    case HttpCode.http_failed:
                        showMsg("连接失败");
                        break;
                    case HttpCode.http_timeout:
                        showMsg("请求超时");
                        break;
                    case HttpCode.QUERY_PRO:
                        try {
                            JSONObject jsonObject = new JSONObject(msg.obj.toString());
                            JSONArray jsonArray = jsonObject.getJSONArray("data");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                BankAdress bankAdress = new BankAdress();
                                JSONObject json = jsonArray.getJSONObject(i);
                                bankAdress.setName(json.getString("PROVINCE"));
                                list.add(bankAdress);
                            }
                            listPro .addAll(list) ;
                            setList(listPro);
                        } catch (Exception e) {
                            e.printStackTrace();

                        }
                        break;
                    case HttpCode.QUERY_CITY:
                        try {
                            isCity = true;
                            JSONObject jsonObject = new JSONObject(msg.obj.toString());
                            JSONArray jsonArray = jsonObject.getJSONArray("data");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                BankAdress bankAdress = new BankAdress();
                                JSONObject json = jsonArray.getJSONObject(i);
                                bankAdress.setName(jsonArray.getJSONObject(i).getString("CITY"));
                                list.add(bankAdress);
                            }
                            setList(list);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                }
            }
        };
    }

    /**
     * 置空listView
     */
    private void setList(List<BankAdress> lists) {
        bankAddressAdapter.setList(null);
        bankAddressAdapter.setList(lists);
        list.clear();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            if (isCity) {
                isCity = false;
                setList(listPro);
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected Class<QuickPayOneDeleagte> getDelegateClass() {
        return QuickPayOneDeleagte.class;
    }


}
